<template>
  <div class="table">
    <el-table
      :data="tableData"
      :loading="loading"
      style="width: 100%"
      :header-cell-style="{
        background: '#F1F1F1',
        'font-size': '14px',
        'font-weight': 600,
        color: '#333333',
      }"
      :cell-style="{
        'font-size': '14px',
        'font-weight': 400,
        color: '#333333',
      }"
    >
      <el-table-column label="序号" min-width="60" fixed="left">
        <template #default="scope">
          {{ (scope.$index + 1).toString().padStart(2, "0") }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="角色名称" min-width="120" />
      <el-table-column prop="mebNum" label="授权人数（个）" min-width="140" />
      <el-table-column label="启用状态" min-width="80">
        <template #default="scope">
          <el-switch
            width="50px"
            :inline-prompt="true"
            v-model="scope.row.status"
            :active-value="true"
            :inactive-value="false"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" min-width="60" />
      <el-table-column label="操作" width="120" fixed="right">
        <template #default="scope">
          <el-button
            @click="empowerData(scope.row)"
            type="primary"
            size="small"
            link
            style="color: #007aff; font-size: 14px"
            >赋权</el-button
          >
          <el-button
            @click="editData(scope.row)"
            type="primary"
            size="small"
            link
            style="color: #007aff; font-size: 14px"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { reactive, ref, defineProps } from "vue";
import { getStatusAPi } from "../request.js";
const imgNone = new URL("@/assets/image/imgNone.png", import.meta.url).href;
const emits = defineEmits(["table"]);

const props = defineProps({
  tableData: {
    type: Object,
    default: () => {},
  },
  loading: {
    type: Boolean,
    default: false,
  },
});

// 启用/停用状态按钮
const handleStatusChange = (row) => {
  ElMessageBox.confirm(
    `您确认要${row.status == true ? "启用" : "停用"}（${row.name}）吗？`,
    "温馨提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(async () => {
      await getStatusAPi({
        id: row.id,
        status: row.status,
      }).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
        } else if (res.data.code === -1) {
          row.status = row.status === true ? false : true;
        }
      });
    })
    .catch(() => {
      row.status = row.status === true ? false : true;
    });
};

// 赋权按钮
const empowerData = (row) => {
  row.operation = "empower";
  emits("table", row);
}

// 编辑按钮
const editData = (row) => {
  row.operation = "edit";
  emits("table", row);
};
</script>

<style lang="less" scoped>
.table {
  margin-bottom: 15px;
}
</style>
